import com.fz.Dao.BookDAO;
import com.fz.entity.Book;
import com.fz.mapper.BookMapper;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by webrx on 2017-09-12.
 */
public class Demo {
    @Test
    public void mm(){
        //两种方法
        /*BookMapper bk=ctx.getBean(BookMapper.class);
        System.out.println(bk.queryById(6));*/
        ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        /*BookMapper bdao=ctx.getBean(SqlSession.class).getMapper(BookMapper.class);
        Book bk=bdao.queryById(6);*/
        BookMapper bk=ctx.getBean(BookMapper.class);
        System.out.println(bk.queryById(6));
        System.out.println(bk);
        //添加方法一
        /*Book b=new Book();
        b.setName("张三分");
        b.setPrice(110);
        b.setBdate(new Date());
        System.out.println(bdao.save(b));*/
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        //2017-09-12 16:09:13
        //添加方法二
       // bdao.save1("张新星",199,new Date());
        ctx.close();
    }
    @Test
    public void zz(){
        ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        BookMapper bdao=ctx.getBean(SqlSession.class).getMapper(BookMapper.class);
        List<Map<String,Object>> li=bdao.query();
        for (Map<String,Object> m:li){
            System.out.println(m.get("id"));
            System.out.println(m.get("name"));
            System.out.println(m.get("price"));
        }
        ctx.close();

    }

@Test
    public void dd(){
        ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        BookDAO bdao=ctx.getBean(BookDAO.class);
        Book book=bdao.queryById(6);
        System.out.println(book);
        List<Book> list=bdao.queryAll();
        for (Book b:list){
            System.out.println(b.getName());
        }

        ctx.close();
    }

    //删除的方法
    @Test
    public void ee(){
        ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        BookDAO bdao=ctx.getBean(BookDAO.class);
        //System.out.println(bdao.deleteById(20));
        bdao.jdbc();
        ctx.close();
    }
}
